package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.PurchaseListService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Sanmier
 * @create 2020-12-29 12:49
 */
@Service
public class PurchaseListServiceImpl implements PurchaseListService {

    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;

    @Autowired
    private GoodsDao goodsDao;


    //新增进货单
    @Override
    public ServiceVO purchaseListGoodsListSave(PurchaseList purchaseList, List<PurchaseListGoods> purchaseListGoodsList) {

        //保存新增进货单
        purchaseListGoodsDao.savePurchaseList(purchaseList);
        Integer purchaseListId = purchaseList.getPurchaseListId();

        //保存商品信息
        for (PurchaseListGoods purchaseListGoods : purchaseListGoodsList) {
            //设置进货单id
            purchaseListGoods.setPurchaseListId(purchaseListId);
            //调用保存方法
            purchaseListGoodsDao.savePurchaseListGoods(purchaseListGoods);
            //查询商品库存  状态
            Goods puGoods = goodsDao.findByGoodsId(purchaseListGoods.getGoodsId());
            puGoods.setLastPurchasingPrice(puGoods.getPurchasingPrice());
            puGoods.setPurchasingPrice(purchaseListGoods.getPrice());

            //设置新总库存
            puGoods.setInventoryQuantity(puGoods.getInventoryQuantity() + purchaseListGoods.getGoodsNum());
            puGoods.setState(2);
            //修改
            goodsDao.updateGoods(puGoods);
        }

        return new ServiceVO<>(SuccessCode.SUCCESS_CODE,SuccessCode.SUCCESS_MESS);
    }


    //条件查询
    @Override
    public Map<String, Object> getSearchList(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        //创建map集合
        Map<String, Object> map = new HashMap<>();
        //查询结果返回列表
        List<PurchaseList> purchaseListList = purchaseListGoodsDao.getSearchList(purchaseNumber, supplierId, state, sTime, eTime);
        //将查询结果放入map
        map.put("rows", purchaseListList);
        //返回map数据
        return map;
    }


    //查询进货单商品信息
    @Override
    public Map<String, Object> getGoodsList(Integer purchaseListId) {
        //创建map集合
        Map<String, Object> map = new HashMap<>();

        //查询结果返回对象
        List<PurchaseListGoods> purchaseListGoods = purchaseListGoodsDao.getGoodsList(purchaseListId);
        map.put("rows",purchaseListGoods);
        return map;
    }


    //进货单删除
    @Override
    public ServiceVO deletePurchaseListGoods(Integer purchaseListId) {
        purchaseListGoodsDao.deletePurchaseListGoods(purchaseListId);
        purchaseListGoodsDao.deletePurchaseList(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }


    //支付结算（修改进货单付款状态）
    @Override
    public ServiceVO updatePurchaseListGoods(Integer purchaseListId) {
        purchaseListGoodsDao.updatePurchaseList(purchaseListId);
        return new ServiceVO(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS);
    }

}
